<template>
  <div class="contentbox">
    <el-card class="box-card">
      <el-button type="primary" plain size="small" icon="el-icon-plus"
        >新增</el-button
      >
      <el-table
        :data="tableData"
        style="width: 100%; margin: 20px 0"
        row-key="id"
        border
        :tree-props="{ children: 'children' }"
      >
        <el-table-column prop="title" label="菜单名称" sortable width="180">
        </el-table-column>
        <el-table-column prop="path" label="菜单地址" sortable width="180">
        </el-table-column>
        <el-table-column prop="mark" label="权限标识"> </el-table-column>
        <el-table-column prop="menu" label="图标" align="center">
          <template #default="{ row }">
            <i :class="`el-icon-${row.icon}`"></i>
          </template>
        </el-table-column>
        <el-table-column prop="component" label="组件路径"> </el-table-column>

        <el-table-column prop="component" label="类型" align="center">
          <template #default="{ row }">
            <el-button
              :type="row.children ? 'primary' : 'warning'"
              plain
              size="mini"
              >{{ row.children ? '菜单' : '按钮' }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="component" label="是否显示" align="center">
          <template #default="{ row }">
            <el-switch
              :value="row.is_show ? true : false"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column
          prop="component"
          label="操作"
          width="300"
          align="center"
        >
          <template #default="{ row }">
            <el-button type="primary" icon="el-icon-edit" circle></el-button>
            <el-button type="warning" icon="el-icon-plus" circle></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              @click="delMenu(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { getAllMenuAPI, delMenuAPI } from '@/api/authorityManagement/menu'
export default {
  data () {
    return {
      tableData: []
    }
  },
  methods: {
    /**
     * 删除菜单项
     */
    delMenu (id) {
      this.$confirm('您确定需要删除此菜单项嘛？', '提示')
        .then(async () => {
          await delMenuAPI(id)
          this.$message.success('删除成功！')
          this.getAllMenu()
        })
        .catch(() => {
          console.log('我也不确定')
        })
    },
    /**
     * 获取权限菜单
     */
    async getAllMenu () {
      const { data } = await getAllMenuAPI()
      this.tableData = data
    }
  },
  created () {
    this.getAllMenu()
  }
}
</script>

<style scoped lang="scss">
.contentbox {
  margin: 20px;
}
</style>
